/**
 *  This file is part of TaoBaoItemImporter.
 *
 *   TaoBaoItemImporter is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   TaoBaoItemImporter is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with TaoBaoItemImporter.  If not, see <http://www.gnu.org/licenses/>. 
 */

package com.googlecode.ulysses.taobao.importer;

import java.util.ArrayList;

import com.googlecode.ulysses.taobao.Constants;
import com.googlecode.ulysses.taobao.Constants.ItemType;
import com.googlecode.ulysses.taobao.data.Item;
import com.googlecode.ulysses.taobao.data.PropertyNameParser;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;

/**
 * @author Hegen <lhegen@gmail.com>
 *
 */
public class BookItemImporter extends ItemImporter {
	private ItemType type = ItemType.ITEM_TYPE_BOOK;

	/* (non-Javadoc)
	 * @see com.googlecode.ulysses.taobao.ItemMongoWriter#addItemPropertyToCollection(com.mongodb.DBCollection, com.googlecode.ulysses.taobao.data.Item)
	 */
	@Override
	public DBCollection addItemPropertyToCollection(DBCollection coll, Item item) {
		BasicDBObject itemProperties = new BasicDBObject();
		doc.put("item_properties", itemProperties);
		// TODO populate itemProperties by using the data in the props_name and props fields
		String property = "";
		String dummy = "";
		PropertyNameParser propNameParser = new PropertyNameParser(item.getItem_get_response().getItem().getProps_name());
		propNameParser.CreatePidVidValue(";", ":");
		itemProperties.put(property, propNameParser.getPropertyValue(dummy));
		return coll;
	}

	/* (non-Javadoc)
	 * @see com.googlecode.ulysses.taobao.ItemMongoWriter#addItemBuyLinkToCollection(com.mongodb.DBCollection, com.googlecode.ulysses.taobao.data.Item)
	 */
	@Override
	public DBCollection addItemBuyLinkToCollection(DBCollection coll, Item item) {
		ArrayList<BasicDBObject> buyLinks = new ArrayList<BasicDBObject>();
		BasicDBObject buyLink = new BasicDBObject();
		buyLink.put("url", Constants.DEFAULT_URL);
		buyLink.put("website", Constants.DEFAULT_WEBSITE);
		buyLink.put("merchant", Constants.DEFAULT_MERCHANT);
		buyLink.put("price", item.getItem_get_response().getItem().getPrice());
		buyLink.put("cash_back", Constants.DEFAULT_CASH_BACK);
		buyLink.put("is_sold_out", Constants.DEFAULT_IS_SOLD_OUT);
		buyLinks.add(buyLink); // FIXME use the real data in the Item object
		doc.put("buy_link", buyLinks); 
		
		return coll;
	}
	
	/* (non-Javadoc)
	 * @see com.googlecode.ulysses.taobao.importer.ItemImporter#addAndUpdateItemToCollection(com.mongodb.DBCollection, com.googlecode.ulysses.taobao.data.Item)
	 */
	@Override
	public DBCollection addAndUpdateItemFieldToCollection(DBCollection coll,
			Item item) {
		doc.put("item_main_cats", type.getItemTypeNumber());
		return coll;
	}

}
